package com.ruoyi.unit.service.impl;

import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.mapper.HouseDoorTMapper;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.unit.domain.HouseUnitT;
import com.ruoyi.unit.mapper.HouseUnitTMapper;
import com.ruoyi.unit.service.IHouseUnitTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 单元管理Service业务层处理
 *
 * @author lijie
 * @date 2023-04-19
 */
@Service
public class HouseUnitTServiceImpl implements IHouseUnitTService {
    @Autowired
    private HouseUnitTMapper houseUnitTMapper;
    @Autowired
    private HouseDoorTMapper houseDoorTMapper;


    /**
     * 查询单元管理
     *
     * @param unitId 单元管理主键
     * @return 单元管理
     */
    @Override
    public HouseUnitT selectHouseUnitTByUnitId(Long unitId) {
        return houseUnitTMapper.selectHouseUnitTByUnitId(unitId);
    }

    /**
     * 查询单元管理列表
     *
     * @param houseUnitT 单元管理
     * @return 单元管理
     */
    @Override
    public HouseUnitT selectUnit(HouseUnitT houseUnitT) {
        return houseUnitTMapper.selectUnit(houseUnitT);
    }

    /**
     * 查询单元管理列表
     *
     * @param houseUnitT 单元管理
     * @return 单元管理
     */
    @Override
    public List<HouseUnitT> selectHouseUnitTList(HouseUnitT houseUnitT) {
        return houseUnitTMapper.selectHouseUnitTList(houseUnitT);
    }

    /**
     * 新增单元管理
     *
     * @param houseUnitT 单元管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseUnitT(HouseUnitT houseUnitT) {
        return houseUnitTMapper.insertHouseUnitT(houseUnitT);
    }

    /**
     * 修改单元管理
     *
     * @param houseUnitT 单元管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHouseUnitT(HouseUnitT houseUnitT) {
        HouseUnitT houseUnitT1 = houseUnitTMapper.selectHouseUnitTByUnitId(houseUnitT.getUnitId());
        houseUnitT1.setAreaName(houseUnitT.getAreaName());
        houseUnitT1.setGroupName(houseUnitT.getGroupName());
        houseUnitT.setUnitName(houseUnitT1.getUnitName());
        HouseUnitT houseUnitT2 = houseUnitTMapper.selectHouseUnitT(houseUnitT1);
        if(houseUnitT2==null){
            houseUnitTMapper.insertHouseUnitT(houseUnitT1);
        }
        List<HouseDoorT> doorList = houseUnitTMapper.selectHouseUnitDoorT(houseUnitT);
        for(HouseDoorT houseDoorT : doorList){
            houseDoorT.setAreaName(houseUnitT.getAreaName());
            houseDoorT.setGroupName(houseUnitT.getGroupName());
            houseUnitT.setUnitName(houseDoorT.getUnitName());
            houseUnitT.setDoorName(houseDoorT.getDoorName());
            HouseDoorT houseDoorT1 = houseDoorTMapper.selectDoor(houseDoorT);
            if(houseDoorT1==null){
                houseUnitTMapper.updateHouseUnitDoorT(houseUnitT);
            }
        }
        return 1;
    }

    /**
     * 批量删除单元管理
     *
     * @param unitIds 需要删除的单元管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseUnitTByUnitIds(Long[] unitIds) {
        return houseUnitTMapper.deleteHouseUnitTByUnitIds(unitIds);
    }

    /**
     * 删除单元管理信息
     *
     * @param unitId 单元管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseUnitTByUnitId(Long unitId) {
        return houseUnitTMapper.deleteHouseUnitTByUnitId(unitId);
    }

    @Override
    public HouseUnitT getDoorNumber(HouseUnitT houseUnitT) {
        return houseUnitTMapper.getDoorNumber(houseUnitT);
    }


    @Override
    public HouseUnitT getMust(HouseUnitT houseUnitT) {
        return houseUnitTMapper.getMust(houseUnitT);
    }

    @Override
    public HouseUnitT getAlready(HouseUnitT houseUnitT) {
        return houseUnitTMapper.getAlready(houseUnitT);
    }

    @Override
    public HouseUnitT getGroupCost(HouseUnitT houseUnitT) {
        return houseUnitTMapper.getGroupCost(houseUnitT);
    }

    @Override
    public List<HouseUnitT> selectHouseUnitTByUnitName(HouseUnitT houseUnitT) {
        return houseUnitTMapper.selectHouseUnitTByUnitName(houseUnitT);
    }

    @Override
    public List<HouseUnitT> selectHouseDoorTByDoorName(HouseUnitT houseUnitT) {
        return houseUnitTMapper.selectHouseDoorTByDoorName(houseUnitT);
}
}
